package com.linln.modules.system.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.modules.system.dao.UserAddressMapper;
import com.linln.modules.system.dao.WxUserMapper;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.entity.UserAddress;
import com.linln.modules.system.entity.WxUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AddressServices {

    @Autowired
    private UserAddressMapper userAddressMapper;

    public Page<UserAddress> getUserAddress(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        Page<UserAddress> pager = userAddressMapper.getAllAddressPager();
        return pager;
    }

    public UserAddress selectById(String id) {
        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(id);
        return userAddress;
    }

    public void saveAddress(UserAddress userAddress) {

        if(userAddress.getId() == null){
            userAddress.setCreateTime(new Date());
            userAddress.setId(UUID.randomUUID().toString());
            userAddressMapper.insert(userAddress);

        }else{
            UserAddress beUser = userAddressMapper.selectByPrimaryKey(userAddress.getId());
            String[] fields = {"createTime"};
            EntityBeanUtil.copyProperties(beUser, userAddress, fields);
            userAddressMapper.updateByPrimaryKey(userAddress);
        }
    }

    public void delete(String addressId) {

        userAddressMapper.deleteByPrimaryKey(addressId);
    }

    @Transactional
    public void updateStatus(StatusEnum statusEnum, List<String> ids) {

        ids.forEach(p->{
            userAddressMapper.deleteByPrimaryKey(p);
        });

    }
}
